// ignore_for_file: unused_local_variable, non_constant_identifier_names

import 'dart:io';

import 'package:dart_bean/config/config.dart';
import 'package:dart_bean/generator.dart';
import 'package:dart_bean/json_to_bean/util.dart';
import 'package:test/test.dart';

/// error 'Int' -> int
void main() {
  final beanFile = File('bean.dart');
  Generator.config = parseConfig(Directory('test').parent);
  final folder = 'test_case/json_files';
  File file;
  test('base_types', () async {
    file = File('$folder/base_types.json');
    testJsonToBean(file);
    final c = beanFile.readAsStringSync();
    expect(c.contains('late int id;'), true);
    expect(c.contains('late bool bool1;'), true);
    expect(c.contains('late double double1;'), true);
    expect(c.contains('late dynamic null1;'), true);
  });

  test('merge_main_type', () async {
    file = File('$folder/merge_main_type.json');
    testJsonToBean(file);
    final c = beanFile.readAsStringSync();
    expect(c.contains('int? a;'), true);
    expect(c.contains('bool? b;'), true);
    expect(c.contains('double? c;'), true);
    expect(c.contains('List<int>? d;'), true);
  });
  test('merge_subtype', () async {
    file = File('$folder/merge_subtype.json');
    testJsonToBean(file);
    final c = beanFile.readAsStringSync();
    expect(c.contains('late List<dynamic> a;'), true);
    expect(c.contains('late List<bool?> b;'), true);
  });
  test('merge_main_and_subtype', () async {
    file = File('$folder/merge_main_and_subtype.json');
    testJsonToBean(file);
    final c = beanFile.readAsStringSync();
    expect(c.contains('List<int?>? a;'), true);
  });
  test('upper_case_field', () async {
    Generator.config.flattenBean = true;
    file = File('$folder/upper_case_field.json');
    testJsonToBean(file);
    final c = beanFile.readAsStringSync();
    expect(c.contains("@JSONField(name: 'Para')"), true);
    expect(c.contains("late String para;"), true);
    expect(c.contains("@JSONField(name: 'GlossSeeAlso')"), true);
    expect(c.contains("late GlossSeeAlso glossSeeAlso;"), true);

    expect(c.contains("class GlossSeeAlso {"), true);
    expect(c.contains("@JSONField(name: 'GML')"), true);
    expect(c.contains("late int gML;"), true);
  });

  test('flatten_bean', () async {
    Generator.config.flattenBean = true;
    file = File('$folder/flatten_bean.json');
    testJsonToBean(file);
    final c = beanFile.readAsStringSync();
    expect(c.contains("late Glossary glossary;"), true);
    expect(c.contains("late GlossDefList glossDefList"), true);
  });

  test('prefix_bean', () async {
    Generator.config.flattenBean = false;
    file = File('$folder/not_flatten_bean.json');
    testJsonToBean(file);
    final c = beanFile.readAsStringSync();
    expect(c.contains("late UserGlossary glossary;"), true);
    expect(c.contains("late UserGlossaryGlossDivGlossListGlossEntryGlossDefList glossDefList;"), true);
  });
}

void testJsonToBean(File file) {
  jsonToBean(file.readAsStringSync(), File('bean.dart'), 'User');
}
